//
//  announcementVC.m
//  Inforcollection
//
//  Created by ocean on 16/10/11.
//  Copyright © 2016年 oceansoft.liw. All rights reserved.
//

#import "announcementVC.h"
#import "AnnounTableCell.h"
#import "UIView+Ext.h"
#import "AnnounceContextCell.h"
@interface announcementVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIButton*submiteBtn;
    NSArray*annountmentArray;
}
@end

@implementation announcementVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self setNaviBarTitle:@"公告详情"];
    [self setNavBarColor:APPNavibackColor];
    annountmentArray=@[@"标        题：",@"发  布  人：",@"接收机构：",@"发布时间：",@"发布内容：",@"签收"];
    UITableView*AnnoucountceMentTable=[[UITableView alloc]initWithFrame:CGRectMake(0, APPNaviHeight, ScreenWidth, ScreenHeight-APPNaviHeight) style:UITableViewStylePlain];
    AnnoucountceMentTable.allowsSelection=NO;
    AnnoucountceMentTable.delegate=self;
    AnnoucountceMentTable.dataSource=self;
    [self.view addSubview:AnnoucountceMentTable];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 10;
    }else if (section==5){
        return 20;
    }
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==4) {
        return 200;
    }
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
   
    return 0.0000000001;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return annountmentArray.count;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *HeadView=[[UIView alloc]init];
    return HeadView;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==4) {
        AnnounceContextCell*cell=[[[NSBundle mainBundle]loadNibNamed:@"AnnounceContextCell" owner:nil options:nil]lastObject];
        cell.Context.text=_reveiveArray[indexPath.section];
        cell.Context.layer.borderWidth=0.5;
        cell.Context.layer.cornerRadius=5.0;
        cell.Context.layer.borderColor=[[UIColor darkGrayColor] colorWithAlphaComponent:0.6].CGColor;
        cell.Context.userInteractionEnabled=YES;
        cell.Context.editable=NO;
        return cell;
    }else{
    AnnounTableCell*cell=[[[NSBundle mainBundle]loadNibNamed:@"AnnounTableCell" owner:nil options:nil]lastObject];
    if(indexPath.section==annountmentArray.count-1){
        [cell.contentView removeAllSubviews];

        submiteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [cell.contentView addSubview:submiteBtn];
        submiteBtn.backgroundColor=AppNavColor;
        [submiteBtn setTitle:@"签收" forState:0];
        [submiteBtn addTarget:self action:@selector(getAnnouncementSigneReceiving) forControlEvents:UIControlEventTouchUpInside];
        submiteBtn.layer.cornerRadius=5.0;
        [submiteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(-5);
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
        }];
        if (self.isChecked==NO) {
            //未签收 。按钮可以点击
            submiteBtn.enabled=YES;
            
        }else{
            submiteBtn.enabled=NO;
            [submiteBtn setBackgroundColor:[UIColor lightGrayColor]];
        }
    }else{
        
        cell.TiteLabl.text=annountmentArray[indexPath.section];
        cell.DetailFileds.text=[NSString stringWithFormat:@" %@",_reveiveArray[indexPath.section]];
        
        cell.DetailFileds.layer.borderWidth=0.5;
        cell.DetailFileds.layer.cornerRadius=5.0;
        cell.DetailFileds.layer.borderColor=[[UIColor darkGrayColor] colorWithAlphaComponent:0.6].CGColor;
        cell.DetailFileds.enabled=NO;
       }
        return cell;

    }
}

//公告签收
-(void)getAnnouncementSigneReceiving{
    
    NSMutableDictionary*info=[NSMutableDictionary dictionary];
    [info setValue:_noticeReveiveRecordId forKey:@"noticeReveiveRecordId"];
    [MBProgressHUD showMessag:@"正在签收..." toView:self.view];
    [[HttpAPI shareHttpAPI]getAnnouncementSignafterreceiving:info CallBack:^(NSDictionary *receivedData) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([receivedData[@"code"]integerValue]==200) {
            
        if ([receivedData[@"status"] integerValue]==1) {
        [MBProgressHUD showSuccess:receivedData[@"desc"] toView:self.view];
        [submiteBtn setBackgroundColor:[UIColor lightGrayColor]];
            
            if (self.refreshData != nil) {
                self.refreshData(@"");
            }
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            [MBProgressHUD showError:@"签收失败，请稍后尝试。" toView:self.view];
        }

    }else{
        [MBProgressHUD showError:@"网络错误" toView:self.view];
    }
           }];
}
- (void)returnText:(ReturnrefreshData)block {
    self.refreshData = block;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    if (self.refreshData != nil) {
//        self.refreshData(self.inputTF.text);
    }
}
-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

@end
